﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using Core.Component;

namespace Components
{
    public partial class SGVToolbar : UserControl, ISGVControl
    {
        public BaseModule module;
        public BaseEntity entity;

        SGVScreen ParentScreen;

        public SGVToolbar()
        {
            InitializeComponent();
            
            this.Load += new EventHandler(SGVToolbar_Load);
            this.ParentChanged += new EventHandler(SGVToolbar_ParentChanged);
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        void SGVToolbar_Load(object sender, EventArgs e)
        {
            //this.toolStrip1.BackColor = Color.FromArgb(255, 232, 166);
            this.toolStrip1.BackColor = Color.Silver;
        }

        String _sGVShortcutString;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }

        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        public void RegisterModule(BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
                this.module.ActionStart += new ActionStartHandler(module_ActionStart);
            }
        }

        void module_ActionStart(object sender, EventArgs e)
        {
            if (module.Action == ActionType.None)
            {
                this.NEW.Enabled = true;
                this.EDIT.Enabled = true;
                this.SAVE.Enabled = false;
                this.CANCEL.Enabled = false;
                this.DELETE.Enabled = false;
                this.LIST.Enabled = true;
                this.PRINT.Enabled = true;
                this.HISTORY.Enabled = true;
            }
            else if (module.Action == ActionType.New)
            {
                this.NEW.Enabled = false;
                this.EDIT.Enabled = false;
                this.SAVE.Enabled = true;
                this.CANCEL.Enabled = true;
                this.DELETE.Enabled = false;
                this.LIST.Enabled = false;
                this.PRINT.Enabled = false;
                this.HISTORY.Enabled = false;
            }
            else if (module.Action == ActionType.Edit )
            {
                this.NEW.Enabled = false;
                this.EDIT.Enabled = false;
                this.SAVE.Enabled = true;
                this.CANCEL.Enabled = true;
                this.DELETE.Enabled = true;
                this.LIST.Enabled = false;
                this.PRINT.Enabled = true;
                this.HISTORY.Enabled = true;
            }
        }

        void SGVToolbar_ParentChanged(object sender, EventArgs e)
        {
            ParentScreen = (SGVScreen)this.Parent;
            RegisterModule(ParentScreen.module);
        }

        #region Event
        public void PRINT_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (module != null)
                module.ActionPrint();
        }

        public void HISTORY_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.History();
        }

        

        public void NEW_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.ActionNew();
        }

        public void EDIT_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.ActionEdit();
        }

        public void CANCEL_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.ActionCancel();
        }

        public void SAVE_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.ActionSave();
        }

        private void DELETE_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.ActionDelete();
        }

        public void LIST_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.List();
        }

        public void CLOSE_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.Close();

        }

        private void PRE_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.Pre();
        }

        private void NEXT_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.Next();
        }

        private void Import_Click(object sender, EventArgs e)
        {
            if (module != null)
                module.Import();
        }

        public void PERMISION_Click(object sender, EventArgs e)
        {

        } 
        #endregion

        public void Init()
        {
        }


        public void UpdateFormat()
        {
        }





        public Core.Component.UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
